package com.edu.controller;

import com.edu.bean.Admin;
import com.edu.bean.Book;
import com.edu.mapper.BookMapper;
import com.edu.service.AdminService;
import com.edu.service.BookService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {

    @Resource
    private AdminService adminService;
    @Resource
    private BookService bookService;

    @RequestMapping("/login")
    public Object login(Admin admin, Model model, HttpSession session){
        Admin loginAdmin = adminService.getAdminByAcc(admin.getAdminAcc());
        if (loginAdmin == null) {
            model.addAttribute("msg","账户不存在");
            return "login";
        }
        if (!loginAdmin.getAdminPass().equals(admin.getAdminPass())) {
            model.addAttribute("msg","密码不正确");
            return "login";
        }
        session.setAttribute("admin",loginAdmin);

        PageHelper.startPage(1,5);
        List<Book> books = bookService.getAllBooks();
        Page<Book> p = (Page<Book>) books;
        model.addAttribute("books",books);
        model.addAttribute("pageInfo",p.toPageInfo());
        return "book";
    }
}
